package com.example.vo

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.MenuItem
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import com.example.vo.Controller.ActivityCollector.finishAll
import com.example.vo.Controller.BaseActivity
import com.example.vo.Data.DB
import com.example.vo.Data.Tword
import com.example.vo.Data.getc_db
import com.example.vo.Data.getn_db
import org.w3c.dom.Text

class TestActivity : BaseActivity() {
    private var lastClickTime = 0L
    private val FAST_TIME = 1000
    private val SPLASH_DISPLAY_LENGHTS = 800 //延迟
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        val toolbar: androidx.appcompat.widget.Toolbar = findViewById(R.id.toolbar_t)
        toolbar.title = "正在测试"
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        val test_m_n: TextView = findViewById(R.id.test_m_n)
        val test_t_1: TextView = findViewById(R.id.test_t_1)
        val test_t_2: TextView = findViewById(R.id.test_t_2)
        val test_t_3: TextView = findViewById(R.id.test_t_3)
        val test_t_4: TextView = findViewById(R.id.test_t_4)
        val tested_num: TextView = findViewById(R.id.tested_num)
        val notest_num: TextView = findViewById(R.id.notest_num)
        val progressBar: ProgressBar = findViewById(R.id.progressbar) //设置进度条

        var item = intent.getStringExtra("item")
        var num = intent.getStringExtra("num")?.toInt()
        var maxnum = intent.getStringExtra("maxnum")?.toInt()
        var rightnum = intent.getStringExtra("rightnum")?.toInt()
        var onum = intent.getStringExtra("onum")?.toInt()
        var confirm = intent.getStringExtra("confirm")?.toInt()

        if (item == null || num == null || maxnum == null || rightnum == null || onum == null || confirm == null) { //全局判空处理
            item = "考研"
            num = 5
            maxnum = 5
            rightnum = 5
            onum = 0
            confirm = 0
        }

        progressBar.max = maxnum //设置进度条最大值
        progressBar.progress = onum //设置进度条进度
        tested_num.text = " " + "已测词数" + onum
        notest_num.text = "未测词数" + maxnum.minus(onum) + " "
        val alltrans_: ArrayList<Tword> //获取对应题库题目
        alltrans_ = gettest(item)

        val trans = mutableListOf(
            alltrans_[0].trans,
            alltrans_[0].trans_1,
            alltrans_[0].trans_2,
            alltrans_[0].trans_3
        )

        trans.shuffle() //打乱数组

        test_m_n.text = alltrans_[0].word //分别设置TextView内容
        test_t_1.text = trans[0]
        test_t_2.text = trans[1]
        test_t_3.text = trans[2]
        test_t_4.text = trans[3]

        val tsm = alltrans_[0].word
        val ts1 = trans[0]
        val ts2 = trans[1]
        val ts3 = trans[2]
        val ts4 = trans[3]

        test_t_1.setOnClickListener {
            progressBar.incrementProgressBy(1)
            num -= 1 //词汇数减一
            if (System.currentTimeMillis() - lastClickTime >= FAST_TIME) { //防止多次点击
                val item_ = getn_db(item).which //转换对应题库表名
                if (testtest(item_, ts1, tsm)) { //判断答案是否正确
                    rightnum += 1
                    test_t_1.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    test_t_1.isClickable = false //设置不可点击
                    test_t_2.isClickable = false
                    test_t_3.isClickable = false
                    test_t_4.isClickable = false
                    Handler().postDelayed(Runnable() {
                        if (num == 0) { //结束逻辑
                            val intent = Intent(this, ResultActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        } else {
                            val intent = Intent(this, TestActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("num", num.toString())
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("onum", (maxnum - num).toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        }
                    }, SPLASH_DISPLAY_LENGHTS.toLong())
                } else {
                    test_t_1.setBackgroundColor(Color.parseColor("#A9F44336"))
                    test_t_1.isClickable = false //设置不可点击
                    test_t_2.isClickable = false
                    test_t_3.isClickable = false
                    test_t_4.isClickable = false
                    if (testtest(item_, ts2, tsm)) {
                        test_t_2.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    } else if (testtest(item_, ts3, tsm)) {
                        test_t_3.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    } else if (testtest(item_, ts4, tsm)) {
                        test_t_4.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    }
                    Handler().postDelayed(Runnable() {
                        if (num == 0) { //结束逻辑
                            val intent = Intent(this, ResultActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        } else {
                            val intent = Intent(this, TestActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("num", num.toString())
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("onum", (maxnum - num).toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        }
                    }, SPLASH_DISPLAY_LENGHTS.toLong())
                }
                lastClickTime = System.currentTimeMillis()
            }
        }
        test_t_2.setOnClickListener {
            progressBar.incrementProgressBy(1)
            num -= 1 //词汇数减一
            if (System.currentTimeMillis() - lastClickTime >= FAST_TIME) { //防止多次点击
                val item_ = getn_db(item).which //转换对应题库表名
                if (testtest(item_, ts2, tsm)) { //判断答案是否正确
                    rightnum += 1
                    test_t_2.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    test_t_1.isClickable = false //设置不可点击
                    test_t_2.isClickable = false
                    test_t_3.isClickable = false
                    test_t_4.isClickable = false
                    Handler().postDelayed(Runnable() {
                        if (num == 0) { //结束逻辑
                            val intent = Intent(this, ResultActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        } else {
                            val intent = Intent(this, TestActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("num", num.toString())
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("onum", (maxnum - num).toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        }
                    }, SPLASH_DISPLAY_LENGHTS.toLong())
                } else {
                    test_t_2.setBackgroundColor(Color.parseColor("#A9F44336"))
                    test_t_1.isClickable = false //设置不可点击
                    test_t_2.isClickable = false
                    test_t_3.isClickable = false
                    test_t_4.isClickable = false
                    if (testtest(item_, ts1, tsm)) {
                        test_t_1.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    } else if (testtest(item_, ts3, tsm)) {
                        test_t_3.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    } else if (testtest(item_, ts4, tsm)) {
                        test_t_4.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    }
                    Handler().postDelayed(Runnable() {
                        if (num == 0) { //结束逻辑
                            val intent = Intent(this, ResultActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        } else {
                            val intent = Intent(this, TestActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("num", num.toString())
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("onum", (maxnum - num).toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        }
                    }, SPLASH_DISPLAY_LENGHTS.toLong())
                }
                lastClickTime = System.currentTimeMillis()
            }
        }
        test_t_3.setOnClickListener {
            progressBar.incrementProgressBy(1)
            num -= 1 //词汇数减一
            if (System.currentTimeMillis() - lastClickTime >= FAST_TIME) { //防止多次点击
                val item_ = getn_db(item).which //转换对应题库表名
                if (testtest(item_, ts3, tsm)) { //判断答案是否正确
                    rightnum += 1
                    test_t_3.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    test_t_1.isClickable = false //设置不可点击
                    test_t_2.isClickable = false
                    test_t_3.isClickable = false
                    test_t_4.isClickable = false
                    Handler().postDelayed(Runnable() {
                        if (num == 0) { //结束逻辑
                            val intent = Intent(this, ResultActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        } else {
                            val intent = Intent(this, TestActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("num", num.toString())
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("onum", (maxnum - num).toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        }
                    }, SPLASH_DISPLAY_LENGHTS.toLong())
                } else {
                    test_t_3.setBackgroundColor(Color.parseColor("#A9F44336"))
                    test_t_1.isClickable = false //设置不可点击
                    test_t_2.isClickable = false
                    test_t_3.isClickable = false
                    test_t_4.isClickable = false
                    if (testtest(item_, ts1, tsm)) {
                        test_t_1.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    } else if (testtest(item_, ts2, tsm)) {
                        test_t_2.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    } else if (testtest(item_, ts4, tsm)) {
                        test_t_4.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    }
                    Handler().postDelayed(Runnable() {
                        if (num == 0) { //结束逻辑
                            val intent = Intent(this, ResultActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        } else {
                            val intent = Intent(this, TestActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("num", num.toString())
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("onum", (maxnum - num).toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        }
                    }, SPLASH_DISPLAY_LENGHTS.toLong())
                }
                lastClickTime = System.currentTimeMillis()
            }
        }
        test_t_4.setOnClickListener {
            progressBar.incrementProgressBy(1)
            num -= 1 //词汇数减一
            if (System.currentTimeMillis() - lastClickTime >= FAST_TIME) { //防止多次点击
                val item_ = getn_db(item).which //转换对应题库表名
                if (testtest(item_, ts4, tsm)) { //判断答案是否正确
                    rightnum += 1
                    test_t_4.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    test_t_1.isClickable = false //设置不可点击
                    test_t_2.isClickable = false
                    test_t_3.isClickable = false
                    test_t_4.isClickable = false
                    Handler().postDelayed(Runnable() {
                        if (num == 0) { //结束逻辑
                            val intent = Intent(this, ResultActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        } else {
                            val intent = Intent(this, TestActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("num", num.toString())
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("onum", (maxnum - num).toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        }
                    }, SPLASH_DISPLAY_LENGHTS.toLong())
                } else {
                    test_t_4.setBackgroundColor(Color.parseColor("#A9F44336"))
                    test_t_1.isClickable = false //设置不可点击
                    test_t_2.isClickable = false
                    test_t_3.isClickable = false
                    test_t_4.isClickable = false
                    if (testtest(item_, ts1, tsm)) {
                        test_t_1.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    } else if (testtest(item_, ts2, tsm)) {
                        test_t_2.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    } else if (testtest(item_, ts3, tsm)) {
                        test_t_3.setBackgroundColor(Color.parseColor("#7A5CFF7F"))
                    }
                    Handler().postDelayed(Runnable() {
                        if (num == 0) { //结束逻辑
                            val intent = Intent(this, ResultActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        } else {
                            val intent = Intent(this, TestActivity::class.java).apply {
                                putExtra("item", item)
                                putExtra("num", num.toString())
                                putExtra("maxnum", maxnum.toString())
                                putExtra("rightnum", rightnum.toString())
                                putExtra("onum", (maxnum - num).toString())
                                putExtra("confirm", confirm.toString())
                            }
                            startActivity(intent)
                            finish()
                            overridePendingTransition(0, 0)
                        }
                    }, SPLASH_DISPLAY_LENGHTS.toLong())
                }
                lastClickTime = System.currentTimeMillis()
            }
        }
    }

    fun gettest(item: String): ArrayList<Tword> { //获取题目
        val item_ = getc_db(item).which //转换对应题库表名
        val twordlist = ArrayList<Tword>()
        val dbHelper = DB(this, "AllTWord.db", 1)
        val db = dbHelper.writableDatabase
        val check = "select * from " + item_ + " order by random() limit " + 1
        val cursor = db.rawQuery(check, null)
        if (cursor.moveToFirst()) {
            do {
                val word = cursor.getString(cursor.getColumnIndex("word"))
                val trans = cursor.getString(cursor.getColumnIndex("trans"))
                val trans_1 = cursor.getString(cursor.getColumnIndex("trans_1"))
                val trans_2 = cursor.getString(cursor.getColumnIndex("trans_2"))
                val trans_3 = cursor.getString(cursor.getColumnIndex("trans_3"))
                twordlist.add(Tword(word, trans, trans_1, trans_2, trans_3))
            } while (cursor.moveToNext())
        }
        cursor.close()
        return twordlist
    }

    fun getttest(): ArrayList<Tword> { //获取题目
        val item_ = "t_kaoyan" //转换对应题库表名
        val twordlist = ArrayList<Tword>()
        val dbHelper = DB(this, "AllTWord.db", 1)
        val db = dbHelper.writableDatabase
        val check = "select * from " + item_ + " order by random() limit " + 1
        val cursor = db.rawQuery(check, null)
        if (cursor.moveToFirst()) {
            do {
                val word = cursor.getString(cursor.getColumnIndex("word"))
                val trans = cursor.getString(cursor.getColumnIndex("trans"))
                val trans_1 = cursor.getString(cursor.getColumnIndex("trans_1"))
                val trans_2 = cursor.getString(cursor.getColumnIndex("trans_2"))
                val trans_3 = cursor.getString(cursor.getColumnIndex("trans_3"))
                twordlist.add(Tword(word, trans, trans_1, trans_2, trans_3))
            } while (cursor.moveToNext())
        }
        cursor.close()
        return twordlist
    }

    fun testtest(item: String, teststring: String, word: String): Boolean { //判断正确
        var retrans = ""
        val dbHelper = DB(this, "AllWord.db", 1)
        val db = dbHelper.writableDatabase
        val check = "select trans from " + item + " where word = '" + word + "'"
        val cursor = db.rawQuery(check, null)
        if (cursor.moveToFirst()) {
            do {
                val trans = cursor.getString(cursor.getColumnIndex("trans"))
                retrans = trans
            } while (cursor.moveToNext())
        }
        cursor.close()
        if (retrans.equals(teststring)) {
            return true
        }
        return false
    }

    fun count() {
        return
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                finish()
            }
        }
        return true
    }
}